package com.feishi.core.util.common.bean;

import java.util.*;

/**
 * @version 1.0
 * @auth caixq
 * @time 18-4-25 10:12
 **/
@SuppressWarnings("unchecked")
public class WaveData {
    private final int maxSize;
    private List<Double> data = new ArrayList();
    private Map<Double, Integer> map = new HashMap();
    private double fluctuationAmplitude = 1.1D;
    int i;
    private Double max;

    public WaveData(int maxSize) {
        this.maxSize = maxSize;
    }

    public WaveData(int maxSize, double fluctuationAmplitude) {
        this.maxSize = maxSize;
        this.fluctuationAmplitude = fluctuationAmplitude;
    }

    public void addData(Double t) {
        this.data.add(t);
        this.map.put(t, (this.map.get(t) == null ? 0 : (Integer)this.map.get(t)) + 1);
        if (this.data.size() > this.maxSize) {
            Double first = (Double)this.data.remove(0);
            if ((Integer)this.map.get(first) == 1) {
                this.map.remove(first);
            } else {
                this.map.put(first, (Integer)this.map.get(first) - 1);
            }
        }

    }

    public boolean isWaveData(Double c) {
        Double unity = this.getUnity();
        if (unity == null) {
            return true;
        } else {
            double tmp = c / unity;
            return tmp > this.fluctuationAmplitude || tmp < 1.0D / this.fluctuationAmplitude;
        }
    }

    public Double getUnity() {
        int maxNum = -2147483648;
        Iterator var2 = this.map.entrySet().iterator();

        while(var2.hasNext()) {
            Map.Entry<Double, Integer> tIntegerEntry = (Map.Entry)var2.next();
            if ((Integer)tIntegerEntry.getValue() > maxNum) {
                maxNum = (Integer)tIntegerEntry.getValue();
                this.max = (Double)tIntegerEntry.getKey();
            }
        }

        return this.max;
    }

    public boolean addCommonData(double count) {
        boolean result=isWaveData(count);
        if(result&&data.size()>=maxSize){

        }else {
            addData(count);
        }

        return result;
    }
}
